import { areaList } from '@vant/area-data';
Page({
  getList(){
    // wx.showLoading({
    //   title: '数据加载中...',
    // })
    this.setData({
      loading:true,
      noData:false,
      list:[]
    })
    wx.cloud.callFunction({
      name:"list",
      data:{
        action:"getList",
        data:{
          limit:this.data.limit,
          skip:this.data.skip,
          filter:{
            houseType:this.data.houseType,
            site:this.data.site
          },
          sort:{
            rent:this.data.sort
          }
        }
      }
    }).then(({result:{code,list,msg}})=>{
      console.log("list  ",list);
      console.log("code  ",code);
      
      if(code===0){
        this.setData({
          list:list
        })
      }else{
        this.setData({
          noData:true
        })
        wx.showToast({
          title: msg,
          icon:"none"
        })
      }

    }).finally(()=>{
      this.setData({
        loading:false
      })
    })
  },
  addList(){
    this.setData({
      loading:true,
      skip:this.data.skip+this.data.limit
    })
    wx.cloud.callFunction({
      name:"list",
      data:{
        action:"getList",
        data:{
          limit:this.data.limit,
          skip:this.data.skip,
          filter:{
            houseType:this.data.houseType,
            site:this.data.site
          },
          sort:{
            rent:this.data.sort
          }
        }
      }
    }).then(({result:{list,code,msg}})=>{
      if(code===0){
        this.setData({
          list:this.data.list.concat(list)
        })
        wx.hideLoading()
      }else{
        this.setData({
          noData:true
        })
        wx.showToast({
          title: msg,
          icon:"none"
        })
      }
    }).finally(()=>{
      this.setData({
        loading:false
      })
    })
  },
  onLoad(){
    const thia=this
    
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        wx.serviceMarket.invokeService({
          service:"wxc1c68623b7bdea7b",
          api:"rgeoc",
          data:{
            "location":latitude+","+longitude
          }
        }).then(({data:{result}})=>{
            console.log("result  ",result);
            console.log(
              result.address_component.province+
              result.address_component.city+
              result.address_component.district);
            console.log("thia.  ",thia.data.adderss);
            
            thia.setData({
              region:result.address_component.district,
              adderss:result.ad_info.adcode,
              site:result.address_component.province+
              result.address_component.city+
              result.address_component.district
            })
            
            thia.getList()
        })
      }
     })
  },
  goDetails(e){
    wx.navigateTo({
      url: '/pages/details/details?_id='+e.currentTarget.dataset.item._id,
    })
  },
  setAddress({detail:{values}}){
    
    console.log("e  ",values.map(item=>item.name).join(""));
    this.setData({
      region:values[2].name,
      site:values.map(item=>item.name).join("")
    })
    this.selectComponent('#item').toggle()
    this.getList()
  },
  data: {
    noData:false,
    adderss:"",
    loading:false,
    skip:0,
    limit:6,
    list:[],
    region:"地区",
    option1: [
      { text: '地区', value: 0 },
      { text: '新款商品', value: 1 },
      { text: '活动商品', value: 2 },
    ],
    sortArr: [
      { text: '默认排序', value: 0 },
      { text: '租金高到低', value: -1 },
      { text: '租金低到高', value: 1 },
    ],
    option3: [
      { text: '房型', value: 0 },
      { text: '好评排序', value: 1 },
      { text: '销量排序', value: 2 },
    ],
    option4: [
      { text: '小区', value: 0 },
      { text: '好评排序', value: 1 },
      { text: '销量排序', value: 2},
    ],
    value1: 0,
    sort: 0,
    value3: 0,
    value4: 0,
    houseType:{
      room:"",
      hall:"",
      kitchen:"",
      restRoom:""
    },
    room:"",
    hall:"",
    kitchen:"",
    restRoom:"",
    areaList : areaList
  },
  qd(){
    this.setData({
      houseType:{
        room:this.data.room,
        hall:this.data.hall,
        kitchen:this.data.kitchen,
        restRoom:this.data.restRoom
      },
    })
    
    this.selectComponent('#houseTpye').toggle()
    this.getList()
  },
  cz(){
    
    this.setData({
      houseType:{
        room:"",
        hall:"",
        kitchen:"",
        restRoom:""
      },
      room:"",
      hall:"",
      kitchen:"",
      restRoom:""
    })
    this.selectComponent('#houseTpye').toggle()
    this.getList()
  },
  closed(){
    this.setData({
      room:this.data.houseType.room,
      hall:this.data.houseType.hall,
      kitchen:this.data.houseType.kitchen,
      restRoom:this.data.houseType.restRoom
    })
  },
  setSort({detail:value}){
    this.setData({
      skip:0
    })
    this.getList()
  },
  bindscrolltolower(){
    this.addList()
  }
});